//
//  YYMusicAnimationView.swift
//  STMusic
//
//  Created by stoneobs on 17/5/22.
//  Copyright © 2017年 stoneobs. All rights reserved.
//
//**********************正在播放按钮***************************
import UIKit
enum YYMusicAnimationViewStyle :Int{
    //初始化高 - 矮
    case YYMusicAnimationViewStyleHighToBottom = 0;
    //初始化矮 - 高
    case YYMusicAnimationViewStyleBottomToHigh = 1;
    //初始化矮 - 高 - 矮
    case YYMusicAnimationViewStyleMiddleHigh = 2;
    
    //初始化高 - 矮 - 高
    case YYMusicAnimationViewStyleMiddleBottom = 3;
}
class YYMusicAnimationView: UIView {

    //三个私有动画view
    private var view1:UIView?
    
    private var view2:UIView?
    
    private var view3:UIView?
    
    private(set) var style:YYMusicAnimationViewStyle? = YYMusicAnimationViewStyle.YYMusicAnimationViewStyleBottomToHigh{
    
    
        didSet{
         
            self.calculateOriginView();

        }
    }
    //记录动画状态
    private var view1AnimationIsUp:Bool = false;
    private var view2AnimationIsUp:Bool = false;
    private var view3AnimationIsUp:Bool = false;
    var itemColor:UIColor! = UIColor.flatPink {
    
        didSet{
            self.view1?.backgroundColor = itemColor;
            self.view2?.backgroundColor = itemColor;
            self.view3?.backgroundColor = itemColor;
        }
    };
    //timer
    private var disPlayLink:CADisplayLink?;
    //回调
    
    var clicAction:(_ view:UIView) -> Void = {_ in 
    
    }
    
    init(frame: CGRect,style:YYMusicAnimationViewStyle) {
        super.init(frame: frame);
        self.backgroundColor = .clear;
        initSubview();
        
        self.style = style;
        

       
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     // MARK:Config Subview
    func initSubview() {
        //5 15
        self.view1 = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 3, height: 5));
        
        self.view2 = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 3, height: 10));
        
        self.view3 = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 3, height: 15));
        
        self.view2?.centerX = self.width / 2;
        
        self.view1?.right = (self.view2?.left)! - 4;
        
        self.view3?.left = (self.view2?.right)! + 4;
        
        self.addSubview(self.view1!);
        
        self.addSubview(self.view2!);
        
        self.addSubview(self.view3!);
        
        self.view1?.bottom = self.height - 3;
        self.view2?.bottom = self.height - 3;
        self.view3?.bottom = self.height - 3;
        
        self.view1?.backgroundColor = itemColor;
        self.view2?.backgroundColor = itemColor;
        self.view3?.backgroundColor = itemColor;
        self.calculateOriginView();

    }
    func calculateOriginView(){
          // 5 10 15
        switch self.style! {
        case  .YYMusicAnimationViewStyleHighToBottom:
            
            self.view1?.height = 15;
            self.view2?.height = 10;
            self.view3?.height = 5;
            self.view1AnimationIsUp = false;
            self.view2AnimationIsUp = false;
            self.view3AnimationIsUp = true;
            break;
        case  .YYMusicAnimationViewStyleBottomToHigh:
            
            self.view1?.height = 5;
            self.view2?.height = 10;
            self.view3?.height = 15;
            self.view1AnimationIsUp = true;
            self.view2AnimationIsUp = true;
            self.view3AnimationIsUp = false;
            break;
        case  .YYMusicAnimationViewStyleMiddleHigh:
            
            self.view1?.height = 10;
            self.view2?.height = 15;
            self.view3?.height = 5;
            self.view1AnimationIsUp = true;
            self.view2AnimationIsUp = false;
            self.view3AnimationIsUp = true;
            break;
        case  .YYMusicAnimationViewStyleMiddleBottom:
            
            self.view1?.height = 10;
            self.view2?.height = 5;
            self.view3?.height = 10;
            self.view1AnimationIsUp = true;
            self.view2AnimationIsUp = true;
            self.view3AnimationIsUp = false;
            break;
            
        }
        self.view1?.bottom = self.height - 3;
        
        self.view2?.bottom = self.height - 3;
        
        self.view3?.bottom = self.height - 3;
        
        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(tapAction(view:)));
        
        self.addGestureRecognizer(tapGes);
    
    }
    func startAnimation()   {
        self.disPlayLink = CADisplayLink.init(target: self, selector: #selector(disPlayLickAction));
        self.disPlayLink?.preferredFramesPerSecond = 20;//每秒多少次
        self.disPlayLink?.add(to: .main, forMode: .commonModes);
    }
    func endAnimation() {
        //回到初始位置
        self.disPlayLink?.invalidate();
        self.calculateOriginView();
    }
    func disPlayLickAction() -> Void {

       
        self.animation(view: self.view1!,isUp: self.view1AnimationIsUp);
        
        self.animation(view: self.view2!,isUp: self.view2AnimationIsUp);
        
        self.animation(view: self.view3!,isUp: self.view3AnimationIsUp);
    }
    //动画
    private func animation(view:UIView,isUp:Bool) -> Void{
    

        if  view == self.view1 {
            if view.height == 15 {
                self.view1AnimationIsUp = false;
            }else if view.height == 5
            {
                self.view1AnimationIsUp = true;
            }
        }
      
        if  view == self.view2 {
            if view.height == 15 {
                self.view2AnimationIsUp = false;
            }else if view.height == 5
            {
                self.view2AnimationIsUp = true;
            }
        }
        
        if  view == self.view3 {
            if view.height == 15 {
                self.view3AnimationIsUp = false;
            }else if view.height == 5
            {
                self.view3AnimationIsUp = true;
            }
        }

        

        
        if isUp {
           view.height = view.height + 1;
        }else{
             view.height = view.height - 1;
        }
        
        
        view.bottom = self.height - 3;

    }
    //点击事件
    func tapAction(view:UIView) -> Void {
        self.clicAction (view);
    }

}
